package com.example.smart_city_0829.activity.logistics_inquiries;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.logistics_inquiries.LogisticsBanner;
import com.example.smart_city_0829.entity.logistics_inquiries.LogisticsCompany;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeGv;
import com.example.smart_city_0829.view.MaxSizeLv;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class LogisticsInquiriesMainActivity extends BaseActivity {

    private EditText searchEt;
    private Banner banner;
    private MaxSizeGv recommendLogistics;
    private MaxSizeLv otherLogistics;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_logistics_inquiries_main;
    }

    @Override
    protected void initView() {
        searchEt = (EditText) findViewById(R.id.search_et);
        banner = (Banner) findViewById(R.id.banner);
        recommendLogistics = (MaxSizeGv) findViewById(R.id.recommend_logistics);
        otherLogistics = (MaxSizeLv) findViewById(R.id.other_logistics);

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        banner.setDelayTime(3000);
    }

    @Override
    protected void initData() {
        //轮播图
        api.getLogisticsBannerData().enqueue(new NetCallBack<LogisticsBanner>() {
            @Override
            public void onResponse(Call<LogisticsBanner> call, Response<LogisticsBanner> response) {
                if (response.body() == null) return;
                final List<LogisticsBanner.DataBean> data = response.body().getData();
                banner.setImages(
                        data.stream()
                                .map(LogisticsBanner.DataBean::getImgUrl)
                                .collect(Collectors.toList())
                );
                banner.setBannerTitles(
                        data.stream()
                                .map(LogisticsBanner.DataBean::getTitle)
                                .collect(Collectors.toList())
                );
                banner.start();
                banner.startAutoPlay();
            }
        });

        //所有物流公司数据
        api.getLogisticsCompany().enqueue(new NetCallBack<LogisticsCompany>() {
            @Override
            public void onResponse(Call<LogisticsCompany> call, Response<LogisticsCompany> response) {
                if (response.body() == null) return;
                otherLogistics.setAdapter(new ArrayAdapter<>(
                        context,
                        android.R.layout.simple_list_item_1,
                        response.body().getData().stream().map(LogisticsCompany.DataBean::getName).collect(Collectors.toList())
                ));

                recommendLogistics.setAdapter(new GvAdapter(response.body().getData().subList(0, 12), context));
            }
        });
    }

    @Override
    protected void initEvent() {

        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object o, ImageView imageView) {
                Glide.with(context)
                        .load(Constant.BASE_URL + o)
                        .transform(new RoundedCorners(8), new RoundedCorners(8))
                        .into(imageView);
            }
        });

        searchEt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                final String key = searchEt.getText().toString();
                if (key.startsWith(" ")){
                    showToastShort("文字开头含有空格");
                }
                //TODO 检验快递单号合法性



                return true;
            }
            return false;
        });

        //跳转至物流公司详情
        recommendLogistics.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final LogisticsCompany.DataBean item = (LogisticsCompany.DataBean) parent.getAdapter().getItem(position);
                final Intent intent = new Intent(context, LogisticsCompanyDetailActivity.class);
                intent.putExtra("id",item.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "物流查询";
    }

    class GvAdapter extends CommonAdapter<LogisticsCompany.DataBean, CommonAdapter.CommGridViewHolder> {

        public GvAdapter(List<LogisticsCompany.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LogisticsCompany.DataBean data, int position, CommGridViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + data.getImgUrl())
                    .into(viewHolder.im);
            viewHolder.serverName.setText(String.valueOf(data.getName()));
        }

        @Override
        protected CommGridViewHolder getViewHolder() {
            return new CommGridViewHolder();
        }

        @Override
        protected void initView(CommGridViewHolder viewHolder, View convertView) {
            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
            viewHolder.serverName = (TextView) convertView.findViewById(R.id.server_name);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.server_list_item;
        }
    }

    class lvAdapter extends CommonAdapter<LogisticsCompany.DataBean, CommonAdapter.CommTextViewViewHolder> {

        public lvAdapter(List<LogisticsCompany.DataBean> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(LogisticsCompany.DataBean data, int position, CommTextViewViewHolder viewHolder, View convertView, Context context) {
            viewHolder.tv.setText(data.getName());
        }

        @Override
        protected CommTextViewViewHolder getViewHolder() {
            return new CommTextViewViewHolder();
        }

        @Override
        protected void initView(CommTextViewViewHolder viewHolder, View convertView) {
            viewHolder.tv = (TextView) convertView.findViewById(R.id.text);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.text;
        }
    }
}